package com.lyf.core.search;

/**
 * @AUTHOR LYF
 * @DATE 2022/2/10
 * @VERSION 1.0
 * @DESC
 * 一、二分查找
 * 1. 分治思想：熟悉 模块，注意切分过程
 *
 * 2. 相关题目
 */
public class BiSearch {
    //查找下标
    public int biSearch(int []data,int target){
        int low = 0, high = data.length-1;
        while(low <= high){
             int mid = low + (high-low)/2;
             if(target==data[mid]){
                 return mid;
             }else if(target>data[mid]){//在右边查找
                 low = mid+1;
             }else{
                 high = mid-1;
             }
        }
        return -1;
    }

    public static void main(String[] args) {
        BiSearch biSearch = new BiSearch();
        int[]data= new int[5];
        data[0] = 1;
        data[1] = 3;
        data[2] = 4;
        data[3] = 5;
        data[4] = 8;
        int rs = biSearch.biSearch(data,4);
        System.out.println(rs);
    }

}
